<?php namespace Home\Controller;
use Common\Model\Cate;
use Common\Model\Coattribute;
use Common\Model\Commod;
use Common\Model\Commodlist;
use Common\Model\Tatt;
use Hdphp\Controller\Controller;

class DetailController extends Controller{
    public function index(){
//        p(Q('get.coid'));
        $coid=Q('get.coid');
//        获得商品信息详情
        $commodData=Db::table('commodity co')->join('commdetail cd','coid','=','shop_commodity_coid')
            ->join('category cg','co.shop_category_cid','=','cg.cid')
            ->where("coid={$coid}")->first();
            $commodData['coimg']=explode('|', $commodData['coimg']);
//        点击次数加一
        $commodModel=new Commod();
        $commodModel->where("coid={$coid}")->increment('coclick',1);
//        获得商品规格属性数据
        $coattData=Db::table('coattribute ct')->join('typeattribute ta','shop_typeattribute_taid','=','taid')
            ->where("shop_commodity_coid={$coid}")->orderBy('taid','asc')->get();
//        把相同的taid的属性放在一起
        $sameTaid=[];
        foreach($coattData as $u){
            $sameTaid[$u['taid']][]=$u;
        }
//        然后组合名字和属性值
        $finalAttr=[];
        $typeattrModel=new Tatt();
        foreach($sameTaid as $taid=>$v){
            $finalAttr[]=[
              'name'=>$typeattrModel->where("taid={$taid}")->pluck('taname'),
              'tatype'=>$typeattrModel->where("taid={$taid}")->pluck('tatype'),
              'value'=>$v,
            ];
        }
//        获得点击排行的数据
        $clickData=Db::table('commodity cd')->join('category cg','cd.shop_category_cid','=','cg.cid')->where("pid=8")
            ->orderBy('coclick','desc')->limit(5)->get();
//        新品上市数据
        $newcommodData=Db::table('commodity cd')->join('category cg','cd.shop_category_cid','=','cg.cid')->where("pid=8")
            ->orderBy('cotime','desc')->limit(5)->get();
//        同品牌商品
        $bid=Db::table('commodity')->where("coid={$coid}")->pluck('shop_brand_bid');
        $samebrandData=Db::table('commodity')->where("shop_brand_bid={$bid}")->limit(5)->get();
//        获得品牌名字
        $bandName=Db::table('brand')->where("bid={$bid}")->pluck('bname');
//        p($commodData);die;
        View::with('bandName',$bandName);
        View::with('bid',$bid);
        View::with('samebrandData',$samebrandData);
        View::with('newcommodData',$newcommodData);
        View::with('clickData',$clickData);
        View::with('finalAttr',$finalAttr);
        View::with('commodData',$commodData);
        View::make();
    }


//    接收异步
    public function add(){
        $str=Q('post.s');
        $newstr = substr($str,0,strlen($str)-1);
        $commolistModel=new Commodlist();
        $commodData=$commolistModel->where("cogroupid='{$newstr}'")->find();
        if($commodData){
            $coattModel=new Coattribute();
            $commodData['coattaddpri']=$coattModel->where("coattid IN({$newstr})")->lists('coattaddpri');
            $sum=0;
            foreach($commodData['coattaddpri'] as $u){
                $sum+=$u;
            }
            $commodData['coattaddpri']=$sum;
        }
//        p($commodData);
        echo json_encode($commodData);exit;
    }


    /**
     * 购物车添加
     */
    public function addcart(){
//        p(Q('post.'));die;
        $id=Q('post.coid');
        $name=Q('post.name');
        $num=Q('post.num');
        $pri=Q('post.pri');
        $str=Q('post.str');
        $info=Q('post.info');
        $sheng=Q('post.sheng');
        $newstr = substr($str,0,strlen($str)-1);
        $commodData=Db::table('typeattribute')->join('coattribute ca','taid','=','shop_typeattribute_taid')
            ->where("shop_commodity_coid={$id} AND coattid IN($newstr) ")->get();
        $commodityModel=new Commod();
        $commodimg=$commodityModel->where("coid={$id}")->pluck('colistimg');
        foreach($commodData as $k=>$u){
            $options[$k]= "{$u['taname']}:{$u['coattvalue']}";
        }
//        p($commodData);die;

        $data = [
            'id'        =>$id, // 商品 ID
            'name'      =>$name,// 商品名称
            'info'      =>$info,//商品简单描述
            'num'       => $num, // 商品数量
            'price'     => $pri, // 商品价格
            'sheng'     =>$sheng,//商品节省的价钱
            'commodimg'=>$commodimg,//商品图片
            'colizuheid'=>$newstr,//货品的组合id
            'options' => $options//所选商品的规格
        ];
        Cart::add($data);
        $cartboxData=Cart::getAllData();
//        $catnum=Cart::getTotalNums();
//        p($cartboxData);
        echo json_encode($cartboxData);exit;
//        unset($_SESSION['cart']);die;
//        p($_SESSION);
    }


//    立即购买异步接受
    public function buynow(){
        $id=Q('post.coid');
        $name=Q('post.name');
        $num=Q('post.num');
        $pri=Q('post.pri');
        $str=Q('post.str');
        $info=Q('post.info');
        $sheng=Q('post.sheng');
        $newstr = substr($str,0,strlen($str)-1);
        $commodData=Db::table('typeattribute')->join('coattribute ca','taid','=','shop_typeattribute_taid')
            ->where("shop_commodity_coid={$id} AND coattid IN($newstr) ")->get();
        $commodityModel=new Commod();
        $commodimg=$commodityModel->where("coid={$id}")->pluck('colistimg');
        foreach($commodData as $k=>$u){
            $options[$k]= "{$u['taname']}:{$u['coattvalue']}";
        }
        $data = [
            'id'        =>$id, // 商品 ID
            'name'      =>$name,// 商品名称
            'info'      =>$info,//商品简单描述
            'num'       => $num, // 商品数量
            'price'     => $pri, // 商品价格
            'sheng'     =>$sheng,//商品节省的价钱
            'commodimg'=>$commodimg,//商品图片
            'colizuheid'=>$newstr,//货品的组合id
            'options' => $options//所选商品的规格
        ];
        $_SESSION['buynow']=$data;
//        p($_SESSION);die;
    }


}